﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Common
{
    public class CryptoHelper
    { 
        public static string Key = "DKMAB5DE";//加密密钥必须为8位  
        //加密算法  
        public static string MD5Encrypt(string pToEncrypt)
        {
            if (string.IsNullOrEmpty(pToEncrypt))
            {
                return null;
            }
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
            des.Key = Encoding.UTF8.GetBytes(Key);
            des.IV = Encoding.UTF8.GetBytes(Key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            
            byte[] data1 = ms.ToArray();
            
            // 压缩
            StringBuilder retStringBuilder = new StringBuilder();
            //data1 = Compress(data1);
            foreach (byte b in data1)
            {
                retStringBuilder.AppendFormat("{0:X2}", b);
            }
            ms.Close();
            cs.Close();
            return retStringBuilder.ToString();
        }

        //解密算法  
        public static string MD5Decrypt(string pToDecrypt)
        {
            if (string.IsNullOrEmpty(pToDecrypt))
            {
                return null;
            }
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            // 解压缩
            //inputByteArray = Decompress(inputByteArray);

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Key = Encoding.UTF8.GetBytes(Key);
            des.IV = Encoding.UTF8.GetBytes(Key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            string ret = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            cs.Close();
            return ret;
        }

        public static byte[] Compress(byte[] bytes)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                GZipStream Compress = new GZipStream(ms, CompressionMode.Compress);
                Compress.Write(bytes, 0, bytes.Length);

                Compress.Close();

                return ms.ToArray();
            }
        }

        public static byte[] Decompress(Byte[] bytes)
        {
            using (MemoryStream tempMs = new MemoryStream())
            {
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    GZipStream Decompress = new GZipStream(ms, CompressionMode.Decompress);

                    Decompress.CopyTo(tempMs);

                    Decompress.Close();

                    return tempMs.ToArray();
                }
            }
        }
    }
}
